﻿using WebApiTest1.ToolServices;

namespace WebApiTest1.Middlewares
{
    public class FactoryActivatedMiddleware : IMiddleware
    {
        private readonly IMessageWriter _messageWriter;

        /// <summary>
        /// 使用构造函数注入，需要在IOC容器中注册该服务
        /// builder.Services.AddTransient<FactoryActivatedMiddleware>();
        /// </summary>
        public FactoryActivatedMiddleware(IMessageWriter messageWriter)
        {
            _messageWriter = messageWriter;
        }      

        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var keyValue = context.Request.Query["key"];

            if (!string.IsNullOrWhiteSpace(keyValue))
            {
                //_dbContext.Requests.Add(new Request("Factory", keyValue));
                //await _dbContext.SaveChangesAsync();
            }

            await next(context);
        }
        
    }
}
